<?php

namespace Admin\Model;
use Think\Model;
class ComModel extends Model {

    protected $pk = '';
    protected $tableName = '';
    protected $token = '';
    protected $cacheTime = 86400;
    protected $orderby = array(); //针对全部查询出的数据的排序

    public function updateCount($id, $col, $num = 1) {
        $id = (int) $id;

        return $this->execute(" update " . $this->getTableName() . " set {$col} = ({$col} + '{$num}') where " . $this->pk . " = '{$id}' ");
    }

    public function itemsByIds($ids = array()) {
        if (empty($ids))
            return array();
        $data = $this->where(array($this->pk => array('IN', $ids)))->select();
        $return = array();
        foreach ($data as $val) {
            $return[$val[$this->pk]] = $val;
        }
        return $return;
    }

    public function fetchAll($field = '*', $where = array()) {
        $cache = cache(array('type' => 'File', 'expire' => $this->cacheTime));
        if (!$data = $cache->get($this->token)) {
            $result = $this->field($field);
            if (!empty($where)) {
                $result = $result->where($where);
            }
            $result = $result->order($this->orderby)->select();
            $data = array();
            foreach ($result as $row) {
                $data[$row[$this->pk]] = $this->_format($row);
            }
            $cache->set($this->token, $data);
        }
        return $data;
    }

    public function _after_update($records, $options) {
//        $admin = session('admin');
//        $action = __ACTION__;
//        $curfunction = ACTION_NAME;
////        $contorllername = MODULE_NAME;
//        $tablearr = $this->tableName();
//        $tablevale = $tablearr[$options['table']];
////        $tablevale = $tablearr[$contorllername];
//        $actionname = $tablearr[$curfunction];
//        M('SysLog')->add([
//            'admin_id' => $admin['admin_id'],
//            'action' => $action,
//            'remark' => "{$admin['username']}修改了:{$tablevale}{$actionname}栏目内容",
//            'create_time' => time(),
//            'extra' => serialize($records),
//        ]);
    }

    public function _after_insert($records, $options) {
//        $admin = session('admin');
//        $action = __ACTION__;
//         $curfunction = ACTION_NAME;
//        $tablearr = $this->tableName();
//        $tablevale = $tablearr[$options['table']];
//        $actionname = $tablearr[$curfunction];
//        M('SysLog')->add([
//            'admin_id' => $admin['admin_id'],
//            'action' => $action,
//            'remark' => "{$admin['username']}在{$tablevale}{$actionname}增加了内容",
//            'create_time' => time(),
//            'extra' => serialize($records),
//        ]);
    }

    public function cleanCache() {
        $cache = cache(array('type' => 'File', 'expire' => $this->cacheTime));
        $cache->rm($this->token);
    }

    public function _format($data) {
        return $data;
    }

    public function tablename() {
        return array(
            "bao_article" => '资讯视频',
            "bao_setting" => '系统设置',
            "bao_users" => '用户',
            "bao_post" => '动态管理',
            "bao_article" => '资讯视频',
            "bao_question" => '问答',
            "bao_order" => '商城订单',
            "bao_goods" => '商城商品',
            "bao_notification" => '消息提示',
            "bao_admin" => '系统-管理员',
            "bao_menu" => '权限-菜单管理',
            "bao_gift" => '直播-礼物管理',
            "bao_ad" => '广告',
            "bao_message" => '站内信',
            "bao_seo" => '系统-SEO',
            "bao_seo" => '直播-标签管理',
            "bao_withdraw" => '财务-提现管理',
            "bao_user_certification" => '用户-认证管理',
            "integral" => '积分',
            "auth" => '管理员权限',
            "usercertification" => '认证',
            "post" => '动态信息',
            "bao_live_tag" => '直播-礼物标签管理',
            "protocol" => '平台协议',           
        );
    }

}
